<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" append-to-body width="650" destroy-on-close :close-on-click-modal="false" @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-row>
				<el-col :span="12">
					<el-form-item label="显示名称" prop="connName">
						<el-input v-model="form.connName" placeholder="" />
					</el-form-item>
					<el-form-item label="主机地址" prop="connAddress">
						<el-input v-model="form.connAddress" placeholder="127.0.0.1" />
					</el-form-item>
					<el-form-item label="用户名" prop="connUser">
						<el-input v-model="form.connUser" placeholder="root" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="数据类型" prop="connDrive">
						<jd-select v-model="form.connDrive" dict="sys_db_type" dataType="String" clearable filterable style="width: 100%;" placeholder="请选择..."></jd-select>
					</el-form-item>
					<el-form-item label="端口号" prop="connPort">
						<el-input v-model="form.connPort" placeholder="3306" />
					</el-form-item>
					<el-form-item label="密码" prop="connPwd">
						<el-input v-model="form.connPwd" placeholder="123456" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="数据库名" prop="connLib">
				<el-input v-model="form.connLib" placeholder="">
					<template #append>
						<el-button :loading="testLoading" @click="clickTest">测试连接</el-button>
					</template>
				</el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" type="textarea" rows="3" placeholder="" />
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()" :disabled="!isTest">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增连接',
					edit: '编辑连接',
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {},
				//验证规则
				rules: {
					connName:[
						{required: true, message: ''}
					],
					connDrive: [
						{required: true, message: ''}
					],
					connAddress: [
						{required: true, message: ''}
					],
					connPort: [
						{required: true, message: ''}
					],
					
					connUser: [
						{required: true, message: ''}
					],
					connPwd: [
						{required: true, message: ''}
					],
					connLib: [
						{required: true, message: ''},
					],
				},
				testLoading: false,
				isTest: false,
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			// 测试连接
			async clickTest(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.testLoading = true;
						var res = await this.$API.app.dbConn.testConnect.post(this.form);
						if (res.code == 200){
							this.isTest = true;
							this.$message.success(res.msg);
						} else {
							this.$alert(res.msg, "提示", {type: 'error'})
						}
						this.testLoading = false;
					}
				});
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid && this.isTest) {
						this.isSaveing = true;
						var res = {};
						if (this.mode == "add"){
							res = await this.$API.app.dbConn.addConnect.post(this.form);
						} else {
							res = await this.$API.app.dbConn.updateConnect.put(this.form);
						}
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
				Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
